﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Ue.Projects.Plugins;
using Ue.Projects.Plugins.Folders;
using Ue.Projects.Plugins.Models;
using Ue.Eagle.Tests.Runtime.Sample6;

using Ue.Core.FileSystems;

namespace Ue.Eagle.Tests.Runtime.Plugins.Folders
{
    /// <summary>
    /// PluginFolders 的摘要说明
    /// </summary>
    [TestClass]
    public class PluginFolderTests
    {
        private IPluginFolder pluginFolder;

        [TestInitialize]
        public void Initial()
        {
            var folder = new RootFolder();
            var pluginHarvester=new PluginHarvester(folder);

            this.pluginFolder = new PluginFolder(
                new[] { "Plugins" },
                "Plugin.config",
                folder,
                pluginHarvester);
        }

        [DeploymentItem("Runtime/PluginFolders", "Plugins")]
        [TestMethod]
        public void ConfigLoaderFromFoldersShouldBeListed()
        {
            var plugins = this.pluginFolder.AvailablePlugins();

            Assert.AreEqual(3,plugins.Count());
        }

        [DeploymentItem("Runtime/PluginFolders/Sample6", "Plugins/Sample6")]
        [TestMethod]
        public void GetPoints()
        {
            var plugins = this.pluginFolder.AvailablePlugins();
            var pluginDescriptor = plugins.FirstOrDefault(item => item.Name == "Sample6");

            Assert.IsNotNull(pluginDescriptor);

            Assert.AreEqual("Ue.Eagle.Core.Sample6", pluginDescriptor.Id);
            Assert.AreEqual(3, pluginDescriptor.ExtensionPoints.Count());
            Assert.AreEqual("Point1", pluginDescriptor.ExtensionPoints.FirstOrDefault().Name);
            Assert.AreEqual("Ue.Eagle.Core.Sample6.Point1", pluginDescriptor.ExtensionPoints.FirstOrDefault().Id);
        }

        [DeploymentItem("Runtime/PluginFolders/Sample6", "Plugins/Sample6")]
        [TestMethod]
        public void GetExtensions()
        {
            var plugins = this.pluginFolder.AvailablePlugins();

            var pluginDescriptor = plugins.FirstOrDefault(item => item.Name == "Sample6");

            Assert.IsNotNull(pluginDescriptor);

            Assert.AreEqual(1, pluginDescriptor.Extensions.Count());

            var point1Element = pluginDescriptor.Extensions.FirstOrDefault().GetExtensionElement(pointId => pluginDescriptor.ExtensionPoints.FirstOrDefault(p=>p.Id.Equals(pointId))) as Point1Element;
            Assert.IsNotNull(point1Element);
            Assert.AreEqual("Ue.Eagle.Core.Sample6.Point1", point1Element.PointId);
            Assert.AreEqual("pointKey", point1Element.Key);
        }

        public class RootFolder : Folder,Ue.Projects.Plugins.FileSystems.IRootFolder
        {
            public RootFolder()
                : base(new FolderRoot()) { }
        }
    }
}
